﻿using System;
using System.Device.Location;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Shell;
using SkiTracker.ViewModel;

namespace SkiTracker.View
{
    public partial class TrackPositionPage : PhoneApplicationPage
    {
        private MapPolyline _polyline;

        public TrackPositionPage()
        {
            InitializeComponent();

            DataContext = new TrackPositionViewModel();

            ViewModel.Coordinates.CollectionChanged += (sender, args) =>
                {
                    if (ViewModel.IsTimerEnabled)
                        _polyline.Path.Add((GeoCoordinate) args.NewItems[0]);
                        
                    Map.SetView(ViewModel.CurrentLocation, ViewModel.ZoomLevel, ViewModel.Heading, MapAnimationKind.Parabolic);
                };

            ViewModel.PropertyChanged += (sender, args) =>
                {
                    if (args.PropertyName == "IsTimerEnabled")
                        if (ViewModel.IsTimerEnabled) AddMapPolyline();
                };
        }

        private void AddMapPolyline()
        {
            _polyline = new MapPolyline { StrokeColor = Color.FromArgb(99, 255, 0, 0), StrokeThickness = 5 };
            Map.MapElements.Add(_polyline);
            _polyline.Path.Add(ViewModel.CurrentLocation);
        }

        public TrackPositionViewModel ViewModel { get { return DataContext as TrackPositionViewModel; } }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;

            if (!(e.Uri.IsAbsoluteUri && e.Uri.AbsoluteUri == "app://external/"))
                App.CurrentApp.ResetLiveTile();

        }


        #region Demo stuff

        private void _1_Click(object sender, EventArgs e)
        {
            Toggle(0);
            Toggle(1);
        }

        private void _2_Click(object sender, EventArgs e)
        {
            Toggle(2);
            ViewModel.Pitch = 55.0;
        }

        private void _3_Click(object sender, EventArgs e)
        {
            Toggle(3);
            ViewModel.IsHeadingAutomatic = (TimerPanel.Visibility == Visibility.Visible);
        }

        private void _4_Click(object sender, EventArgs e)
        {
            Toggle(4);
        }


        private void Toggle(int idx)
        {
            var controls = new UIElement[] { CoordinatesListbox, Map, MapControl, TimerPanel, Speedometer };

            ToggleVisibility(controls[idx]); 
            if (idx > 0) ToggleButton(idx);
            
            if (idx == 0)
            {
                var isMapVisible = controls[0].Visibility == Visibility.Collapsed;
                EnableAppBarButtons(isMapVisible);
                MapPanel.Visibility = isMapVisible ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        private void ToggleVisibility(UIElement control)
        {
                control.Visibility = control.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
        }
        
        private void ToggleButton(int idx)
        {
            idx--;

            var button = (ApplicationBarIconButton) ApplicationBar.Buttons[idx];
            var iconPath = button.IconUri.OriginalString;
            var isChecked = iconPath.EndsWith("_.png");

            iconPath = isChecked ? iconPath.Replace("_.png", ".png") : iconPath.Replace(".png", "_.png");

            button.IconUri = new Uri(iconPath, UriKind.Relative); 
        }

        private void EnableAppBarButtons(bool isEnabled)
        {
            ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = isEnabled;
            ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = isEnabled;
            ((ApplicationBarIconButton)ApplicationBar.Buttons[3]).IsEnabled = isEnabled;
        }

        #endregion

    }
}